import { useState, useEffect } from "react";

interface SizeOptions {
  max?: number;
  min?: number;
  baseScale?: number;
}

export function useSizeScale(
  baseWidth: number,
  options: SizeOptions = {}
): number {
  const { max, min, baseScale = 1 } = options;
  const [scale, setScale] = useState(1);

  useEffect(() => {
    const handleResize = () => {
      const currentWidth = window.innerWidth;
      let newScale = currentWidth / baseWidth;

      // 检查是否在指定范围内
      if (max !== undefined && currentWidth > max) {
        newScale = baseScale;
      } else if (min !== undefined && currentWidth < min) {
        newScale = baseScale;
      }

      setScale(newScale);
    };

    // 初始计算
    handleResize();

    // 添加事件监听
    window.addEventListener("resize", handleResize);

    // 清理事件监听
    return () => {
      window.removeEventListener("resize", handleResize);
    };
  }, [baseWidth, max, min, baseScale]);

  return scale;
}
